Skip to content

server-functions consistent return between client and server#792

Closed
arbassett wants to merge 4 commits into
solidjs:mainfrom
arbassett:server$-server-res-match-client
Closed

server-functions consistent return between client and server#792
arbassett wants to merge 4 commits into
solidjs:mainfrom
arbassett:server$-server-res-match-client

Conversation

@arbassett
Copy link
Copy Markdown
Contributor

@arbassett arbassett commented Mar 7, 2023

fixes #768

server-functions are inconsistent between server renders and client renders.

When the server does a render server$ is directly invoked

/**
*  currently in the context of the server render you can basically think this as const fn = () => new Data()
*/
const $$server_module0$1 = server$.createHandler(async function $$serverHandler0() {
  return new Date();
}, "/_m/0dbe216f23/routeData", true);
server$.registerHandler("/_m/0dbe216f23/routeData", $$server_module0$1);

and on the client server$ is a fetch to the generate endpoint

/** 
* again you can think of this as const fn = () => fetch('/_m/0dbe216f23/routeData')
*/
const $$server_module0$1 = server$.createFetcher('/_m/0dbe216f23/routeData', false)

So when the server does a SSR render you get a raw Date object back from the server function while on the client its over a network boundary so it gets serialized by the server function middleware. This causes issues when returning none serializable object like Date where on the server you can call toLocaleDateString and get a successful first render but when the client navigates to the page it throws

TRPC went though a similar issue with their types trpc/trpc#3261 and what this PR is based on

  • Make server-functions consistent between client and server
  • update types
    • server$
    • createServerData$
    • createServerAction$
    • createServerMultiAction$
  • How does this effect customer seralizers

@arbassett arbassett changed the title fix: server-functions SSR consistent with client createFetcher fix: server-functions consistent return between client and server Mar 7, 2023
Comment on lines +290 to +296
} else if (
typeof data === "object" ||
typeof data === "string" ||
typeof data === "number" ||
typeof data === "boolean"
) {
return JSON.stringify(data);
Copy link
Copy Markdown
Contributor Author

@arbassett arbassett Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be optimized to only stringify on object but to make the check consistent with repondWith I left it as is

@arbassett arbassett changed the title fix: server-functions consistent return between client and server server-functions consistent return between client and server Mar 7, 2023
@ryansolid
Copy link
Copy Markdown
Member

This is a good point. I wonder if we should just use Solid's serializer that we use for resources. We did something simple initially.

Actually this probably more relevant for Bling (where we are currently in the process of extracting this into a reusable library) and where this will ultimately land.

@arbassett
Copy link
Copy Markdown
Contributor Author

This is a good point. I wonder if we should just use Solid's serializer that we use for resources. We did something simple initially.

Actually this probably more relevant for Bling (where we are currently in the process of extracting this into a reusable library) and where this will ultimately land.

ah now that bling has its own repo ill check that this is handled & create a issue if its not to keep track of it

since this will ultimately be a bling issue ill close this and move work there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Date type mismatch between Client and Server causing Runtime "TypeError"

2 participants